package com.hzlj.position.locate.controller;


import cn.hutool.core.date.DateUtil;
import com.fz.common.base.core.JsonUtils;
import com.fz.common.base.dto.PageParamDTO;
import com.fz.common.base.dto.QueryPageResultDTO;
import com.fz.common.base.dto.ResultDTO;
import com.fz.common.base.office.EasyExcelUtil;
import com.hzlj.position.locate.common.dto.reportVideo.*;
import com.hzlj.position.locate.common.enums.VideoStatus;
import com.hzlj.position.locate.service.ReportVideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 【通信联络核查-视频核验】接口
 *
 * @author lifh
 * @date 2023-03-13 19:10:15
 */
@Slf4j
@RestController
@RequestMapping("/position/report/video")
public class ReportVideoController {
    @Resource
    private ReportVideoService reportVideoService;

    /**
     * 通过id查询通信联络核查-视频核验
     */
    @GetMapping("/{id}")
    public ResultDTO<ReportVideoQueryResultDTO> getReportVideoById(@PathVariable("id") String id) {
        return new ResultDTO<>(reportVideoService.getReportVideoById(id));
    }


    /**
     * 视频的拨打动作
     */
    @PostMapping("/action/call")
    public ResultDTO<ReportVideoCallResultDTO> call(@RequestBody ReportVideoCallDTO dto) {
        log.info("【视频核验】新增发起={}", JsonUtils.obj2json(dto));
        return new ResultDTO<>(
                reportVideoService.saveReportVideo(dto)
        );
    }


    /**
     * 视频的接听动作
     */
    @PostMapping("/action/{action}")
    public ResultDTO<Boolean> action(@PathVariable("action") String action, @RequestBody ReportVideoUpdateDTO dto) {
        log.info("【视频核验】修改状态=action={},{}", action, dto);
        VideoStatus status = VideoStatus.of(action);
        switch (VideoStatus.of(action)) {
            case ANSWER:
            case REJECT:
            case HANGUP:
                reportVideoService.updateReportVideo(status, dto);
                break;
            default:

        }
        return new ResultDTO<>();
    }

    /**
     * 导出 通信联络核查-视频核验
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       ReportVideoQueryParamDTO dto) {
        List<ReportVideoWithJzJbxxDTO> list = reportVideoService.listReportVideo(dto);
        EasyExcelUtil.downCustomerExcel(response,
                "视频核查记录",
                "视频核查记录",
                EasyExcelUtil.Field.<ReportVideoWithJzJbxxDTO>of()
                        .add(ReportVideoWithJzJbxxDTO::getJgMc, "机构")
                        .add(ReportVideoWithJzJbxxDTO::getXm, "姓名")
                        .add(e -> e.getSpotCheckTime() == null ? "/" : DateUtil.formatDateTime(e.getSpotCheckTime()), "核查时间")
                        .add(ReportVideoWithJzJbxxDTO::getVideoStatusText, "核查状态")
                        .add(e -> e.getVideoStartTime() == null ? "/" : DateUtil.formatDateTime(e.getVideoStartTime()), "视频开始时间")
                        .add(e -> e.getVideoEndTime() == null ? "/" : DateUtil.formatDateTime(e.getVideoEndTime()), "视频结束时间")
                        .add(ReportVideoWithJzJbxxDTO::getVideoDurationText, "通话时长")
                        .add(ReportVideoWithJzJbxxDTO::getReportAddress, "位置信息")
                        .getFields(),
                list
        );
    }


    /**
     * 分页查询 通信联络核查-视频核验
     */
    @GetMapping("/list/page")
    public ResultDTO<QueryPageResultDTO<ReportVideoWithJzJbxxDTO>> pageReportVideo(ReportVideoQueryParamDTO dto, PageParamDTO pageParamDTO) {
        return new ResultDTO<>(reportVideoService.pageReportVideo(dto, pageParamDTO));
    }

}

